package com.cci.kangdao.controller;

import com.cci.kangdao.service.EngineMsgService;
import com.cci.kangdao.utilTool.ParameterTool;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//发动机信息controller
@Controller
public class EngineMsgController {
    private Logger log = Logger.getLogger(this.getClass().getName());
    @Autowired
    private EngineMsgService engineMsgService;

    /**
     * 根据发动机型号和服务渠道id获取发动机
     *
     * @param
     * @return
     */
    @RequestMapping("/getEngineMsg.aspx")
    public ModelAndView getEngineMsg(HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return engineMsgService.engineMsgCheckOut(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    /**
     * 根据AMT号和服务渠道id获取发动机信息和变速箱信息
     *
     * @param
     * @return
     * @date 2022-12-20
     * @description dcec amt服务工单需求新增
     */
    @RequestMapping("/getEngineMsgByAmt.aspx")
    public ModelAndView getEngineMsgByAmt(HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return engineMsgService.engineMsgCheckOutByAmt(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }


    //ccec校验发动机信息
    @RequestMapping("/getEngineMsgCCEC.aspx")
    public ModelAndView getEngineMsgCCEC(HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return engineMsgService.engineMsgCheckOutCCEC(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }


    @RequestMapping("/validateEngine.aspx")
    public void validateEngine(HttpServletRequest request,HttpServletResponse response){
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            engineMsgService.validateEngine(map);
        } catch (Exception ex) {
            log.error("设备是否可用接口发生异常："+ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    @RequestMapping("/getEngineMsgByESN.aspx")
    public void getEngineMsgByESN(HttpServletRequest request,HttpServletResponse response){
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            engineMsgService.getEngineMsgByESN(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    //用户添加设备时  校验是否是大运的发动机，是返回相关信息
    @RequestMapping("/checkOutDYEngine.aspx")
    public ModelAndView checkOutDYEngine(HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return engineMsgService.checkOutDYEngine(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    /**
	 * xcec售时工单需求
	 * 从crm查询主机厂、发动机系统、发动机用途
	 */
    @RequestMapping("getOEMInfoFromCrmXCEC.aspx")
    public void getOEMInfoFromCrmXCEC(HttpServletResponse response) {
    	try {
    		JSONObject obj = engineMsgService.getOEMInfoFromCrmXCEC();
    		ParameterTool.writeResponseForNull(response, 0, "success",obj);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ParameterTool.writeResponse(response, 1, e.getMessage());
		}
    }


    /**
	 * xcec售时工单需求
	 * 从crm查询出某个esn对应的主机厂、发动机系统、发动机用途
	 */
    @RequestMapping("getOEMInfoFromCrmByEsnXCEC.aspx")
    public void getOEMInfoFromCrmByEsnXCEC(String esn, HttpServletResponse response) {
    	try {
    		if(StringUtils.isEmpty(esn)) {
    			throw new RuntimeException("参数esn不能为空");
    		}
    		JSONObject obj = engineMsgService.getOEMInfoFromCrmByEsnXCEC(esn);
    		ParameterTool.writeResponseForNull(response, 0, "success",obj);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ParameterTool.writeResponse(response, -1, e.getMessage());
		}
    }


}
